<?php


namespace app\common\dao\system\merchant;


use app\common\dao\BaseDao;
use app\common\model\system\merchant\Article;
use app\common\model\system\merchant\ArticleCategory;
use app\common\model\BaseModel;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;


class ArticleCategoryDao extends BaseDao
{

    /**
     * @return BaseModel
     * @author: fupeng
     * @day: 2023/11/27
     */
    protected function getModel(): string
    {
        return ArticleCategory::class;
    }

    /**
     * @return array
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function getAllOptions()
    {
        return ArticleCategory::getDB()->order('sort DESC')->column('pid,title', $this->getPk());
    }

    /**
     * @param $status
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function getAll($status = null)
    {
        return ArticleCategory::getDB()->when($status,function($query)use($status){
            $query->where('status',$status);
        })->order('sort DESC')->select();
    }

    /**
     * @param array $where
     * @return \think\db\BaseQuery
     * @author: fupeng
     * @day: 2023/11/27
     *
     */
    public function search(array $where)
    {
        return ArticleCategory::getDB()->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
            $query->where('status', $where['status']);
        })->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
            $query->where('pid', $where['pid']);
        })->order('sort DESC, mer_article_category_id DESC');
    }

    /**
     * @param $id
     * @return bool
     * @author: fupeng
     * @day: 2023/11/28
     */
    public function merFieldExists($field, $value, $except = null)
    {
        return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
                $query->where($field, '<>', $except);
            })->where($field, $value)->count() > 0;
    }


    public function merArtcleFieldExists($field, $value, $except = null)
    {
        return (new Article())->where($field, $value)->count() > 0;
    }

    /**
     * @param int $id
     * @param $except
     * @return bool
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function merExists(int $id, $except = null)
    {
        return $this->merFieldExists($this->getPk(), $id, $except);
    }

    /**
     * @param $id
     * @return array|Model|null
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function get($id)
    {
        return ($this->getModel())::getDB()->find($id);
    }

    /**
     * @param int $id
     * @return int
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function delete(int $id)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->delete();
    }
}
